package com.youths.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youths.dao.YouthInfoMapper;
import com.youths.domain.DicomEntity;
import com.youths.domain.HttpResult;
import com.youths.dao.SliceMapper;
import com.youths.domain.Slice;
import com.youths.domain.YouthInfo;
import com.youths.service.SliceService;
import com.youths.util.HttpAPIUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/7/5
 */
@Service
@Slf4j
public class SliceServiceImpl implements SliceService {
    @Autowired(required = false)
    SliceMapper sliceMapper;

    @Autowired(required = false)
    YouthInfoMapper youthInfoMapper;

    @Autowired
    HttpAPIUtil httpAPIUtil;

    @Value("${url.dicomToJpg}")
    private String dicomToJpgUrl;

    @Override
    public int addSlice(String boneDicomPath, String boneImgPath, String httpPath, Float height, Float weight, Date physicalTime, Integer youthId, Integer state) {
        int result = sliceMapper.add(boneDicomPath, boneImgPath, httpPath, height, weight, physicalTime, youthId, state);
        return result;
    }

    @Override
    public int updateSlicePath(String slicingPath, Integer slicingId, Integer state) {
        int result = sliceMapper.updatePath(slicingPath, slicingId, state);
        return result;
    }

    @Override
    public int updateSliceTaskId(Integer taskId, Integer slicingId, Integer state) {
        int result = sliceMapper.updateTask(taskId, slicingId, state);
        return result;
    }

    @Override
    public List<Slice> getSliceList(Integer youthId) {
        List<Slice> list = sliceMapper.list(youthId);
        return list;
    }

    @Override
    public int saveSlice(String boneDicomPath, String boneImgPath, String httpPath, Date physicalTime, Integer state) {
        int result = sliceMapper.save(boneDicomPath, boneImgPath, httpPath, physicalTime, state);
        return result;
    }

    @Override
    public int updateSliceInfo(Float height, Float weight, String sliceingPath,Integer youthId, Integer slicingId,Integer state) {
        int result = sliceMapper.updateInfo(height, weight, sliceingPath, youthId, slicingId, state);
        return result;
    }

    @Override
    public int saveSlice(Slice slice) {
        int result = sliceMapper.save(slice);
        if (result > 0) {
            return slice.getSlicingId();
        }else {
            return 0;
        }
    }

    @Override
    public Slice query(Integer slicingId) {
        return sliceMapper.query(slicingId);
    }

    @Override
    public int updateState(Integer slicingId, Integer state) {
        return sliceMapper.updateState(slicingId, state);
    }

    @Override
    public int uploadDicomWithSave(MultipartFile file) {
        try {
            HttpResult result = httpAPIUtil.doPostWithFile(dicomToJpgUrl, "file", file);
            //HttpResult result = httpAPIUtil.doPost(dicomToJpgUrl); //模拟接口
            if (result.getCode() == 200) {
                log.debug("【JSON】:{}", result.getBody());
                DicomEntity dicomEntity = JSON.parseObject(result.getBody(), DicomEntity.class);
                //DicomEntity dicomEntity = JSON.parseArray(result.getBody(), DicomEntity.class);

                //error=0 true
                if (dicomEntity.getError().equals("0")) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                    Slice slice = new Slice();
                    slice.setBoneDicomPath(dicomEntity.getDicomPath());
                    slice.setBoneImgPath(dicomEntity.getJpgSavePath());
                    slice.setHttpPath(dicomEntity.getHttpPath());
                    if(dicomEntity.getStudyDate().length() > 0) {
                        slice.setPhysicalTime(sdf.parse(dicomEntity.getStudyDate()));
                    }else {
//                        Date date = new Date();
//                        slice.setPhysicalTime(sdf.parse(sdf.format(date)));
                        slice.setPhysicalTime(null);
                    }
                    slice.setState(0);
                    int sliceId = saveSlice(slice);
                    if (sliceId > 0) {
                        log.info("sliceId:" + sliceId);
                        return sliceId;
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int updateTime(Integer slicingId, Date physicalTime) {
        return sliceMapper.updateTime(slicingId, physicalTime);
    }

    @Override
    public PageInfo getSliceList(Integer pageNum, Integer slicingId, String date1, String date2, String card) {
        PageHelper.startPage(pageNum,10);
        List<YouthInfo> youthInfos = null;
        if(card != null && !card.equals("")){
            youthInfos = youthInfoMapper.getList(null,null,card,null);
            if(youthInfos.size()==0){
                return new PageInfo();
            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try{
            if(date2 != null && !date2.equals("")){
                if(date1 != null && !date1.equals("") && simpleDateFormat.parse(date1).after(simpleDateFormat.parse(date2))){
                    String temp = date1;
                    date1 = date2;
                    date2 = temp;
                }
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(simpleDateFormat.parse(date2));
                calendar.add(calendar.DATE,1);
                date2 = simpleDateFormat.format(calendar.getTime());
                System.out.println(date2);
            }

        }catch (Exception e){ }
        List<Slice> slices = sliceMapper.getList(slicingId,date1,date2,youthInfos);
        PageInfo<Slice> slicePageInfo = new PageInfo<>(slices);
        return slicePageInfo;
    }
}
